package com.mehdiuz.backend.role.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mehdiuz.backend.commons.utils.Result;
import com.mehdiuz.backend.role.entity.Menu;
import com.mehdiuz.backend.role.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/menu/")
@Api(tags = "菜单管理")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping("getMenuList")
    @PreAuthorize("hasAuthority('bnt.menu.list')")
    @ApiOperation(value = "获取树形菜单列表")
    public Result<List<Menu>> getMenuList(){
        return Result.ok(menuService.getMenuList());
    }

    @PostMapping("deleteMenu")
    @PreAuthorize("hasAuthority('bnt.menu.delete')")
    @ApiOperation(value = "删除当前菜单")
    public Result<Boolean> deleteMenu(@RequestBody Menu menu){
        boolean b = menuService.removeById(menu.getId());
        if(b){
            return Result.ok(b);
        }else {
            return Result.fail();
        }
    }

    @PostMapping("insertMenu")
    @ApiOperation(value = "新增菜单")
    @PreAuthorize("hasAuthority('bnt.menu.insert')")
    public Result<Boolean> insertMenu(@RequestBody Menu menu){
        boolean insert = menuService.save(menu);
        if(insert){
            return Result.ok(insert);
        }else {
            return Result.fail();
        }
    }

    @PostMapping("updateMenu")
    @ApiOperation(value = "修改当前菜单")
    @PreAuthorize("hasAuthority('bnt.menu.edit')")
    public Result<Boolean> updateMenu(@RequestBody Menu menu){
        boolean b = menuService.updateById(menu);
        if(b){
            return Result.ok(b);
        }else {
            return Result.fail();
        }
    }
}
